using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.Text;
using System.Linq;
/// Create by xhy
/// Time:2024_1_12
/// <summary>
/// 根据sprites图集动画名，自动生成各种序列帧动画片段
/// </summary>

public class Fruit2DAnimEditor : EditorWindow
{
    private static bool needBornPrefab = true;
    //生成出的Prefab的路径
    private static string PrefabPath = "Resources/FruitRogueRes/Battle/Enemy";
    //生成出的AnimationController的路径
    private static string AnimationControllerPath = "Resources/FruitRogueRes/Battle/Enemy";
    //生成出的Animation的路径
    private static string AnimationPath = "Resources/FruitRogueRes/Battle/Enemy";
    //资源文件夹
    private static string ResPath = "";

    /// <summary>
    /// 临时存储int[]
    /// </summary>
    private int[] IntArray = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    //帧率采样数
    private static int FrameRate = 30;
    //是否循环
    private static bool IsLoop = false;
    //要循环的动作名字
    private static string AnimaName = "";

    //Anim Mode
    private int AnimModeInt = 0;
    private string[] AnimModeString = new string[] { "SpriteRenderer"};

    private static Texture atlasSprite;

    //展示说明
    //private static bool showExplain = true;

    //是否翻转动画序列帧
    private static bool isMirrorAniClip = false;
    /// <summary>
    /// 图集动画信息
    /// </summary>
    Dictionary<string, List<string>> animNames = new Dictionary<string, List<string>>();
    private int AnimResInt = 0;
    private List<string> AnimResList = new List<string>();

   [MenuItem("水果肉鸽项目工具/图集序列帧转换AnimClip",false, -10)]
    public static void Init()
    {
        Fruit2DAnimEditor window = (Fruit2DAnimEditor)EditorWindow.GetWindowWithRect(typeof(Fruit2DAnimEditor), new Rect(300,400,500,600),
            true, "制作序列帧动画");
        window.Show();
    }

    private void OnEnable()
    {
        atlasSprite = null;
        ResPath = "";
        AnimResList.Clear();
        animNames.Clear();
    }

    private void OnDestroy()
    {
        AnimResList.Clear();
        animNames.Clear();
        atlasSprite = null;
    }

    /// <summary>
    /// 显示窗体里面的内容
    /// </summary>
    private void OnGUI()
    {

        atlasSprite = (Texture)EditorGUILayout.ObjectField("序列帧图集", atlasSprite, typeof(Texture));
        if (GUI.changed)
        {
            animNames = Fruit2DAnimTools.GetAnimationInfo(atlasSprite);
        }
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        AnimationControllerPath = EditorGUILayout.TextField("Animator生成父路径:", AnimationControllerPath);
        EditorGUILayout.Space();
        AnimationPath = EditorGUILayout.TextField("AnimationClip生成父路径:", AnimationPath);
        EditorGUILayout.Space();
        needBornPrefab = EditorGUILayout.Toggle("是否新生成预制体", needBornPrefab);
        if (needBornPrefab)
        {
            //预制体
            PrefabPath = EditorGUILayout.TextField("预制体生成父路径:", PrefabPath);
        }
        else
        {
            EditorGUILayout.LabelField("    注意！！！ 不生成新预制体记得手动绑定动画资源，给旧预制体");
        }

        EditorGUILayout.Space();
        if(animNames != null && animNames.Count > 0)
        {
            var resInt = new List<int>();
            for (int i = 0; i < animNames.Count; i++)
            {
                resInt.Add(i);
            }
            AnimResList.AddRange(animNames.Keys.ToArray());
            AnimResInt = EditorGUILayout.IntPopup("资源名称", AnimResInt, AnimResList.ToArray(), resInt.ToArray());
            ResPath = AnimResList[AnimResInt];
            EditorGUILayout.Space();

        }
        else
        {
            GUI.enabled = false;
            ResPath = EditorGUILayout.TextField("资源名称:", ResPath);
            GUI.enabled = true;
        }


        AnimModeInt = EditorGUILayout.IntPopup("输出类型", AnimModeInt, AnimModeString, IntArray);
        EditorGUILayout.Space();
        //帧率
        GUILayout.BeginHorizontal();
        GUILayout.Label("序列帧采样数");
        FrameRate = EditorGUILayout.IntSlider(FrameRate, 1, 60);
        GUILayout.EndHorizontal();
        GUI.enabled = false;
        AnimaName = EditorGUILayout.TextField("序列帧前缀:", string.Format("{0}_", ResPath));
        GUI.enabled = true;

        EditorGUILayout.Space(20f);

        //isMirrorAniClip = EditorGUILayout.Toggle("是否翻转动画片段", isMirrorAniClip);

        //开始生成
        if (GUILayout.Button("开始生成Animation"))
        {
            var outPutType = typeof(SpriteRenderer);
            switch (AnimModeInt)
            {
                case 0:
                    outPutType = typeof(SpriteRenderer);
                    break;
                case 1:
                    outPutType = typeof(Image);
                    break;
                default:
                    outPutType = typeof(SpriteRenderer);
                    break;
            }

            Fruit2DAnimTools.Frame2AnimData f2d = new Fruit2DAnimTools.Frame2AnimData(atlasSprite, AnimationControllerPath, AnimationPath,
                PrefabPath, ResPath, AnimaName, FrameRate, IsLoop, needBornPrefab);

            Fruit2DAnimTools.BuildAllAnimation(f2d, outPutType, isMirrorAniClip);
        }

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        if(animNames != null && animNames.Count > 0)
        {
            GUI.enabled = false;
            StringBuilder sb = new StringBuilder();
            foreach (var a in animNames)
            {
                sb.Append(a.Key+": ");
                foreach (var b in a.Value)
                {
                    sb.Append(b+", ");
                }
                sb.Append('\n');
            }
            GUILayout.TextField("需要制作序列帧的动画:\n" + sb.ToString());
            GUI.enabled = true;
        }

        //showExplain = EditorGUILayout.Toggle("查看使用说明", showExplain);
        //if (showExplain)
        //{
        //    GUILayout.TextArea("本插件提供已打包好图集转换为Clip动画功能,自动生成Animator,根据选项可选择是否每次重新生成预制体\n1.选择序列帧的图集\n2.配置父节点路径\n3.如果没有勾选生成预制体请记得把animator文件绑定\n4.输入资源名称（输出文件夹路径)\n5.选择输出类型 分2D和UI两种\n6.序列帧前缀会根据资源名称自动生成，也可手动编辑。确保图集里包含序列帧前缀名称\n7.点击生成资源，去各个路径下去找生成资源");
        //}
    }
}
